﻿using CIS.Web.Common;
using CIS.Web.Models;
using System.Data.Entity;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Linq;
using System.Linq.Dynamic;

namespace CIS.Web.Controllers
{
	[AllowAnonymous]
	[RoutePrefix("MeetingNewsList")]
	public class MeetingNewsListController : BaseDbController
	{
		public async Task<ActionResult> Index(string id)
		{
			var newsListInfo = await DbContext.NewsListInfo.AsNoTracking()
					.FirstOrDefaultAsync(x => x.ID == id && x.Status == ConstValues.Status.正常);

			if (newsListInfo == null)
			{
				return RedirectToAction(nameof(HomeController.MainIndex), nameof(HomeController).GetControllerName());
			}

			var conferenceMaster = await DbContext.ConferenceMaster.AsNoTracking()
					.FirstOrDefaultAsync(x => x.ID == newsListInfo.ConferenceId);

			var model = new MeetingNewsListModel();
			model.Id = id;
			model.ConferenceId = newsListInfo.ConferenceId;
			model.NewsListName = newsListInfo.NewsListName;
			model.NewsStyle = newsListInfo.NewsStyle;
			model.ConferenceNickName = conferenceMaster.ConferenceNickName;
			model.IsInternational = conferenceMaster.IsInternational;
			model.ConferenceName = conferenceMaster.ConferenceName;
			model.Keywords = conferenceMaster.Keywords;
			model.Description = conferenceMaster.Description;
			model.IsShowCountdown = conferenceMaster.IsShowCountdown;
			model.PicForBackgroundPath = string.IsNullOrEmpty(conferenceMaster.PicForBackground) ? string.Empty :
						conferenceMaster.PicForBackground.Replace("~", "").Replace("\\", "/");
			model.OpacityOfPic = conferenceMaster.OpacityOfPic;
			model.IsFollowBackground = conferenceMaster.IsFollowBackground;
			model.ColorForBackground = conferenceMaster.ColorForBackground;
			model.OpacityOfColor = conferenceMaster.OpacityOfColor;
			model.ColorForMenu = conferenceMaster.ColorForMenu;
			model.OpacityOfMenu = conferenceMaster.OpacityOfMenu;
			model.ColorForLeft = conferenceMaster.ColorForLeft;
			model.OpacityOfLeft = conferenceMaster.OpacityOfLeft;
			model.ColorForCenter = conferenceMaster.ColorForCenter;
			model.OpacityOfCenter = conferenceMaster.OpacityOfCenter;
			model.ColorForRight = conferenceMaster.ColorForRight;
			model.OpacityOfRight = conferenceMaster.OpacityOfRight;
			model.ColorForConference = conferenceMaster.ColorForConference;
			model.FontColorForMenu = conferenceMaster.FontColorForMenu;

			var sql = DbContext.NewsInfo.AsNoTracking()
					.Where(x => x.Status == ConstValues.Status.正常 &&
						x.ConferenceId == newsListInfo.ConferenceId &&
						x.NewsListId == id)
					.OrderByDescending(x => x.Stick).
					ThenByDescending(x => x.Sort).
					ThenBy(x => x.CreateDate);

			var sqlResult = await sql.ToListAsync();

			var result = sqlResult.Select(x => new NewsListModel
			{
				Id = x.ID,
				Link = !string.IsNullOrEmpty(x.Link) ? x.Link : ConstValues.SystemWeb + "/MeetingNewsInfo/Index/" + x.ID,
				Title = x.NewsTitle,
				Abstract = x.Abstract,
				MainText = x.MainText,
				SimplePic = x.SimplePic,
				CreateDate = x.CreateDate.Value.ToString("yyyy-MM-dd"),
				MainTextShared = x.MainText == null ? string.Empty : x.MainText.Replace("\r", "").Replace("\n", "").Replace("<p>", "").Replace("</p>", "").Replace("&nbsp;", "")
			});

			model.NewsListModel = result.ToList();

			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName, "Index", model.ConferenceId, model.Id);

			return View(model);
		}
	}
}